// packageLongInsure/pages/dispatch.js
import user from '../../../utils/user'
import {
    getDispatchList,
    revokeDispatch,
    getDispatchExamine,
    deleteDispatch,
    deleteOccupationById,
} from '../../api/long-insure'
import { toast } from '../../../utils/util.js'
const app = getApp()
Page({
    data: {
        dispatchList: [],
        type: '',
        showWithdraw: false,
        statusMap: {
            0: '待审核',
            1: '正常', // 正常 页面不显示
            2: '已驳回',
            3: '已禁用',
        },
        searchValue: '',
        selectIndex: -1,
        withDrawType: 'add',
        withDrawId: '',
        showDelete: false,
        deleteIndex: '',
    },
    static: {
        pageNum: 1,
        hasNextPage: 1,
    },
    async onPullDownRefresh() {
        await this.getDispatch(1)
        wx.stopPullDownRefresh()
    },
    onReachBottom() {
        this.getDispatch()
    },
    onLoad(options) {
        if (options.type === 'radio') {
            this.setData({
                type: 'radio',
            })
        }
    },
    async onShow() {
        await user.waitLogin()
        this.getDispatch(1)
    },
    async getDispatch(clear) {
        if (clear) {
            this.static.pageNum = 1
            this.static.hasNextPage = true
        }
        if (!this.static.hasNextPage) {
            return
        }
        const result = await getDispatchList({
            pageNum: this.static.pageNum,
            pageSize: 100,
            status: this.data.type ? 1 : '',
            searchText: this.data.searchValue,
            isGetFile: 1,
        })
        console.log(result, '100000000000')
        if (clear) {
            this.data.dispatchList = []
        }
        if (!result.hasNextPage) {
            this.static.hasNextPage = 0
        }
        this.static.pageNum += 1
        this.data.dispatchList.push(...result.list)
        this.setData({
            dispatchList: this.data.dispatchList,
        })

        console.log(1111, this.data.dispatchList)
    },
    // 跳转到新增被派遣单位
    onAdd() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/dispatch-add/dispatch-add?addType=isfirstapply',
        })
    },
    // 对被派遣单位操作
    async onMore(event) {
        console.log(event, 'fhuyssgiufosguhi')
        const index = event.currentTarget.dataset.index
        const dispatch = this.data.dispatchList[index]
        const revoke = async () => {
            await revokeDispatch({
                id: dispatch.id,
            })
            app.toast('撤回审核成功')
            this.data.dispatchList.splice(index, 1)
            this.setData({
                dispatchList: this.data.dispatchList,
            })
        }
        const del = async () => {
            await deleteDispatch({
                id: dispatch.id,
            })
            app.toast('删除被派遣单位成功')
            this.data.dispatchList.splice(index, 1)
            this.setData({
                dispatchList: this.data.dispatchList,
            })
        }
        const video = async () => {
            const result = await getDispatchExamine({ id: dispatch.id })
            if (result.fileDetailList) {
                const arr = result.fileDetailList.map((item) => {
                    return {
                        url: item,
                        type: 'video',
                    }
                })
                console.log(arr, 'arrarr')
                wx.previewMedia({
                    sources: arr,
                })
            } else app.toast('该被派遣单位审核暂未上传视频')
        }

        let result
        if (dispatch.status === 1) {
            result = await wx.showActionSheet({
                // ['编辑','查看视频','删除']
                itemList: user.isAdmin ? ['查看视频', '删除'] : ['查看视频'],
            })
        } else if (dispatch.status === 0) {
            result = await wx.showActionSheet({
                itemList: ['查看视频', '撤销被派遣单位'],
            })
            console.log(result, '待审核待审核待审核')
        } else {
            result = await wx.showActionSheet({
                itemList: ['编辑', '查看视频', '撤销被派遣单位'],
            })
        }

        if (dispatch.status === 0) {
            if (result.tapIndex === 0) {
                video()
            }
            if (result.tapIndex === 1) {
                revoke()
            }
        } else if (dispatch.status === 1) {
            if (result.tapIndex === 0) {
                video()
            }
            if (result.tapIndex === 1) {
                del()
            }
        } else {
            if (result.tapIndex === 0) {
                wx.navigateTo({
                    url:
                        '/packageLongInsure/pages/dispatch-add/dispatch-add?id=' +
                        dispatch.id +
                        '&addType=add',
                })
            }
            if (result.tapIndex === 1) {
                video()
            }
            if (result.tapIndex === 2 && dispatch.status === 1) {
                del()
            }
            if (result.tapIndex === 2 && dispatch.status !== 1) {
                revoke()
            }
        }
    },
    /**
     * 显示撤销弹框
     */
    onShowWithdraw(ev) {
        const dataset = ev.currentTarget.dataset
        this.data.selectIndex = dataset.index
        console.log(123, dataset)
        this.setData({
            showWithdraw: true,
            withDrawType: dataset.type,
            withDrawId: dataset.id,
        })
    },
    /**
     * 隐藏撤销弹框
     */
    onWithdrawCancel() {
        console.log(123456)
        this.setData({
            showWithdraw: false,
        })
    },
    /**
     * 点击确认撤销
     */
    async onWithConfirm() {
        if (this.data.withDrawType === 'add') {
            // 撤销派遣单位
            const dispatch = this.data.dispatchList[this.data.selectIndex]
            await revokeDispatch({
                id: dispatch.id,
            })
            app.toast('撤回审核成功')
            this.data.dispatchList.splice(this.data.selectIndex, 1)
            this.setData({
                dispatchList: this.data.dispatchList,
            })
        } else {
            // 撤销新工种
            console.log(1234)
            await deleteOccupationById(this.data.withDrawId)
            toast('撤销成功')
            this.data.dispatchList[
                this.data.selectIndex
            ].occupationRecord = null
            this.setData({
                dispatchList: this.data.dispatchList,
            })
        }
        this.onWithdrawCancel()
    },
    /**
     * 去详情页
     */
    onToDetail(ev) {
        const dataset = ev.currentTarget.dataset
        wx.navigateTo({
            url: `/packageLongInsure/pages/dispatch-detail/dispatch-detail?id=${dataset.id}`,
        })
    },
    /**
     * input输入事件
     * @param {*} ev
     */
    inputchange(ev) {
        console.log(111, ev)
        const dataSet = ev.currentTarget.dataset
        this.setData({
            [dataSet.model]: ev.detail.value,
        })
        this.getDispatch(true)
    },
    onClearAll() {
        this.setData({
            searchValue: '',
        })
        this.getDispatch(true)
    },
    /**
     * 点击申请新工种   重新上传
     * @param {*} ev
     */
    onApply(ev) {
        const dataset = ev.currentTarget.dataset
        wx.navigateTo({
            url: `/packageLongInsure/pages/dispatch-add/dispatch-add?id=${dataset.id}&addType=apply`,
        })
    },
    /**
     * 点击查看视频
     * @param {*} ev
     */
    onWatchVideo(ev) {
        const dataset = ev.currentTarget.dataset
        console.log(5555, dataset)
        // 第一次提交审核（被派遣单位审核）视频从filedetaillist里拿，新工种从occupationRecord里拿
        let fileList = []
        if (dataset.itemdata.status === 0) {
            fileList = dataset.itemdata.fileDetailList || []
        } else {
            fileList = dataset.itemdata?.occupationRecord?.fileDetails.map(
                (i) => i.fileUrl
            )
        }
        console.log(2121, fileList)
        if (!fileList?.length) {
            toast('暂无视频')
            return
        }
        const videos = fileList.map((i) => {
            return {
                url: i,
                type: 'video',
            }
        })
        wx.previewMedia({
            sources: videos,
        })
    },
    onShowCompany(ev) {
        const dataset = ev.currentTarget.dataset
        this.setData({
            showDelete: true,
            deleteIndex: dataset.index,
        })
    },
    onHideDelete() {
        this.setData({
            showDelete: false,
        })
    },
    onHandleDelete() {
        this.del()
    },
    /**
     * 删除企业
     */
    async del() {
        const dispatch = this.data.dispatchList[this.data.deleteIndex]
        await deleteDispatch({
            id: dispatch.id,
        })
        app.toast('删除被派遣单位成功')
        this.onHideDelete()
        this.data.dispatchList.splice(this.data.deleteIndex, 1)
        this.setData({
            dispatchList: this.data.dispatchList,
        })
    },
    /**
     * 重新申请
     * @param {*} ev
     */
    onReapply(ev) {
        const dataset = ev.currentTarget.dataset
        console.log(2121, dataset)
        wx.navigateTo({
            url: `/packageLongInsure/pages/dispatch-add/dispatch-add?id=${dataset.itemdata.id}&addType=reapply`,
        })
    },
})
